<template>
  <div class="cart_container">
    <div class="cart_header">
      购物车
    </div>
    <!-- 购物车数据渲染 -->
    <div class="cart_list_container">
        <div class="cart_item_container" v-for="(item,index) in cartItem" :key="index">
          <div class="cart_item_wrapper">
            <div class="">
              <input type="checkbox" @change="OnChecked(item)" :checked="item.checked">
            </div>
            <div class="cart_item_img">
              <img :src="item.mainPic" alt="">
            </div>
            <div class="cart_item_message">
              <div class="cart_item_title">{{item.title}}</div>
              <div class="cart_item_price">
                ￥{{item.actualPrice}}
              </div>
              <div class="price_container">
                <div @click="OnMinus(item)">-</div>
                <div>{{item.brand}}</div>
                <div @click="OnPlus(item)">+</div>
              </div>
            </div>
          </div>
        </div>
    </div>
    <!-- 下面全选框 -->
    <div class="cart_bottom">
      <div><input type="checkbox" @change="OnAllChecked()" :checked="allSelect">全选</div>
      <div class="total_prcie_container">总价：{{totalPrice}}</div>
    </div>

  </div>
</template>

<script>
export default {
  data(){
    return {
      allSelect:false
    }
  },
  computed:{
    cartItem(){
      return this.$store.state.cartItems
    },
    totalPrice(){
      var tempPrice = 0
      this.cartItem.forEach((ele)=>{
        if(ele.checked){
          tempPrice += ele.brand * ele.actualPrice
        }
      })

      return tempPrice
    }
  },
  methods:{
    OnChecked(item){
      item.checked = !item.checked
      // console.log(item)
      var result = this.cartItem.every((ele)=>{
        return ele.checked === true
      })
      if(result){
        this.allSelect = true
      }else{
        this.allSelect = false
      }
    },
    OnAllChecked(){
      this.allSelect = !this.allSelect

      this.cartItem.forEach((ele)=>{
          ele.checked = this.allSelect
      })
      // if(this.allSelect){
        
      // }else {

      // }
    },
    OnMinus(item){
      item.brand--
    },
    OnPlus(item){
      item.brand++
    }
  }
}
</script>

<style scoped>
  .cart_container {
    width: 100%;
  }
  .cart_header {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid gray;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    font-size: 22px;
  }
  .cart_list_container {
    width: 100%;
    background-color: #f5f5f5;
  }
  .cart_item_container {
    width: 100%;
    height: 140px;
    background-color: #fff;
    margin-top: 10px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
  }
   .cart_item_wrapper {
    width: 94%;
    height: 120px;
    display: inline-flex;
    justify-content: flex-start;
    align-items: center;
  }
  .cart_item_img {
    margin: 0px 10px;
    width: 30%;
    /* border: 1px solid gray; */
    border-radius: 10px;
    overflow: hidden;
  }
  .cart_item_img img {
    width: 100%;
  }
  .cart_item_title {
    width: 200px;
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
    /* background-color: orange; */
  }
  .cart_item_price {
    /* width: ; */
    color: red;
    margin: 15px 0;
  }
  .price_container {
    width: 100%;
    height: 30px;
    display: inline-flex;
    justify-content: flex-start;
    align-items: center;
  }
  .price_container div {
    width: 30px;
    height: 30px;
    border: 1px solid gray;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    grid-column-start: 20px;
  }
  .cart_bottom {
    width: 100%;
    height: 50px;
    background-color: #000;
    display: inline-flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
  }
  .cart_bottom input {
    margin-left: 20px;
  }
  .total_prcie_container {
    /* width: ; */
    margin-right: 20px;
  }
</style>